<?PHP if( ! defined('BASEPATH')) exit('Sorry, no direct script allowed.');

require APPPATH . 'libraries/modelsqlite.php';

class GLDEF extends Modelsqlite
{
	protected 
	
	$glkey = '',
	$gldescr = '';
	
	function __construct()
	{
		parent::__construct();
	}
	
	function create_table($table, $script)
	{
		if(strlen($table) === 0 OR strlen($script) === 0)
		{
			return array("Status" => "ERROR","Message" => "No parameters found. create new table process aborted.");
		}
		
		$conn = NULL;
		$conn = $this->db_connect();
		
		$s = "select * from sqlite_master where type = 'table' and name = '".$table."' LIMIT 1";
		
		$tmd = $conn->query($s);
		$tmd->setFetchMode(PDO::FETCH_OBJ);
		
		while($row = $tmd->fetch())
		{
			$s = "DROP TABLE ".$table;
			$tmd = $conn->prepare($s);
			$tmd->execute();
		}
		
		$s = $script;
		$tmd = $conn->prepare($s);
		$tmd->execute();
			
		$this->db_close();
		
		return array("status" => "OK");
	}
	
	public function insert_entry($params = array())
	{
		if(sizeof($params) === 0)
		{
			return array("Status" => "ERROR","Message" => 'No parameters found. insert process aborted.');
		}
		
		for($i=0; $i<1; $i++)
		{
			if($i === 0)
			{
				$key_id =  $params[$i];
				break;
			}
		}
		
		$conn = NULL;
		$conn = $this->db_connect();
		
		try
		{
			$s = 'DELETE FROM glkey WHERE glkey = ?';
			$tmd = $conn->prepare($s);
			$tmd->execute(array($key_id));
		
			$s = 'INSERT INTO glkey (glkey,gldescr) VALUES (?,?)';
		
			$tmd = $conn->prepare($s);
			$tmd->execute($params);
			
			$output = array("Status" => "OK","record_changes" => $params);
			
		}
		catch(PDOException $e)
		{
			$output = array("Status" => "ERROR","Message" => $e->getMessage());
			file_put_contents('PDOErrors.txt', 
				mktime(time()) .': '. $e->getMessage(), FILE_APPEND);
			
		}
		
		return $output;
	}
	
	public function search_records($search)
	{
		$conn = NULL;
		$conn = $this->db_connect();
		$str = '';
		
		foreach($search as $key => $val)
		{
			if($key !== 0)
			{
				$params = str_replace(',','","',$val);
				switch($key)
				{
					case $key === 'key':
						$str .= ' and glkey in ("'. strtoupper($params) .'") ';
						break;
						
					case $key === 'descr':
						$str .= ' and upper(gldescr) like "'. strtoupper($params) .'%" ';
						break;
				}
				
			}
		}
		
		$s = 'SELECT glkey, gldescr FROM glkey WHERE 1=1 '. $str ;
		
		$tmd = $conn->query($s);
		$tmd->setFetchMode(PDO::FETCH_OBJ);
		
		$response = array();
		$tamp = array();
		
		while($row = $tmd->fetch())
		{
			$tamp[] = array("gl_key" => $row->glkey,"gl_description" => $row->gldescr);
		}
		
		$this->db_close();
		
		return $tamp;
	
	}
	
	public function get_all_entries()
	{
		$conn = NULL;
		$conn = $this->db_connect();	
		
		$s = 'SELECT glkey, gldescr FROM glkey LIMIT 500';
		$query = $conn->query($s);
		
		$query->setFetchMode(PDO::FETCH_OBJ);
		
		$tamp = array();
		$output = array();
		$output['status'] = 'OK';
		
		while($row = $query->fetch())
		{
			$tamp[]= array( 
				'gl_key' => $row->glkey,
				'gl_description' => $row->gldescr
			);
		}
		
		$this->db_close();
		
		return $tamp;
		
	}
	
}
